<?php

class M_pengumuman extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function pengumuman_get_count_rows($query){
		$sql="SELECT * FROM pengumuman
				LEFT JOIN kategori ON (
				pengumuman.kategori_id = kategori.id )
				WHERE publish='y'";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		return $num_rows;
	}

	function getDetail($id){
		$sql = "
			SELECT
				pengumuman.id,
				judul,
				isi_pengumuman,
				pengumuman.created_date,
				tag,
				isi_pengumuman,
				publish,
				image_pengumuman,
				kategori_id,
				kategori
			FROM pengumuman
			JOIN kategori ON pengumuman.kategori_id = kategori.id
			WHERE pengumuman.id = ".$id."
		";
		$query = $this->db->query($sql);

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function get_view($judul){

		$sql="SELECT *,
				dayname(pengumuman.created_date) dayname,
				date_format(pengumuman.created_date,'%d-%m-%Y') tanggal_ina,
				pengumuman.isi_pengumuman as isi_berita
				FROM pengumuman
				LEFT JOIN kategori ON (
				pengumuman.kategori_id = kategori.id )
				WHERE lower(judul)='".strtolower($judul)."'";
		//$this->firephp->log($sql);

		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->row();
		}else{
			return "";
		}
	}

	function pengumuman_listview($query, $start, $end){

		$sql="SELECT pengumuman.*,
				dayname(pengumuman.created_date) dayname,
				date_format(pengumuman.created_date,'%d-%m-%Y') tanggal_ina,
				date_format(pengumuman.created_date,'%Y-%m-%d') tanggal,
				kategori.kategori FROM pengumuman
				LEFT JOIN kategori ON (
				pengumuman.kategori_id = kategori.id )
				WHERE publish='y' ORDER BY id DESC";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		$sql.=" LIMIT $start, $end";
		//$this->firephp->log($sql);

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function pengumuman_list($search_text, $start, $end, $tglawal, $tglakhir){

		$sql="SELECT
				pengumuman.id,
				judul,
				isi_pengumuman,
				kategori,
				publish
			FROM
				pengumuman
			LEFT JOIN kategori ON (
				pengumuman.kategori_id = kategori.id
			)
			WHERE pengumuman.id IS NOT NULL 
			";
		if($search_text != ''){
			$sql .= " AND ( lower(judul) LIKE '%".$search_text."%' 
				
			)";
		}
		if($tglawal != '' && $tglakhir != ''){
			$sql .= " AND pengumuman.created_date >= '".$tglawal."' AND pengumuman.created_date <= '".$tglakhir."' ";
		}
		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}
		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}

	function pengumuman_save($params){
		extract($params);
		if($task == 'create'){
			$this->db->insert('pengumuman',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('pengumuman',$data);
		}
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

	function pengumuman_delete($id){
		$this->db->where('id',$id);
		$this->db->delete('pengumuman');
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function pengumuman_switchpublish($id, $stats){
		$data = array(
			"publish"=>$stats
		);
		$this->db->where("id", $id);
		$this->db->update('pengumuman', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}